home *** CD-ROM | disk | FTP | other *** search
- Path: news.th-darmstadt.de!news!enno
- From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
- Newsgroups: comp.lang.c++
- Subject: Re: Overloading operator->
- Date: 12 Feb 1996 16:05:16 GMT
- Organization: Fachbereich Informatik, TH Darmstadt
- Distribution: world
- Message-ID: <ENNO.96Feb12170516@kitz.inferenzsysteme.informatik.th-darmstadt.de>
- References: <4fmpgd$rko@newsbf02.news.aol.com>
- NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
- In-reply-to: smalherbe@aol.com's message of 12 Feb 1996 02:16:29 -0500
-
- In article <4fmpgd$rko@newsbf02.news.aol.com> smalherbe@aol.com (SMalherbe) writes:
-
- I am attempting to implement a standard "indirection via a proxy"
- design pattern. I do this using the textbook overloading operator->
- approach. This works if the proxy (B in my example) is declared to
- be either automatic or static, but not on the heap. This is a
- significant limitation. The problem appears to be that I cannot
- do a "real" dereference and then use my overloaded operator without
- doing it explicitly (ie. "(*B)->"). This seems pretty ugly. Is there
- a way around this or am I missing something?
-
- class A {
- public:
- A () : x (0) {}
- int f() const {return x;}
- private:
- int x;
- };
-
- class B {
- public:
- B () {aPtr = new A;}
- A *operator->() {return aPtr;}
- private:
- A *aPtr;
- };
-
- main ()
- {
- B myB;
- B *myBPtr = new B;
-
- myB->f(); // <--- This is OK
- myBPtr->f(); // <--- This produces the following error:
-
- // proxy.cpp(23:12) : error EDC3079: "f" is not a member of "B".
-
- (*myBPtr)->f(); // <--- Is also OK
- }
-
- Thanks in advance for any advice,
-
- You cannot overload the operators for pointers. That's the core of
- your problem. However the following snippet is perfectly valid with
- the new behavior of 'new':
-
- B& myBRef=*new B;
- myBRef->f();
- delete &myBRef;
-
- Enno
-
-